<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../include/taglib.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="com.sinodata.bsm.common.vo.Probe"%>
<%@ page import="com.sinodata.bsm.common.constants.RelationConstants"%>

<%
            response.setHeader("Cache-Control", "no-store");
			response.setHeader("Pragrma", "no-cache");
			response.setDateHeader("Expires", 0);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>资源新增</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<%@ include file="../include/common.jsp"%>
<%@ include file="../include/tree.jsp"%>

<script type=text/javascript>

	function back() {
		var backUrl = document.referrer;
		location.assign(backUrl);
	}
	
	function refreshViewTree()
	{		
		var tree = window.parent.parent.getResTree();
		var nodes = tree.getSelectedNodes();
		if($("#isRoot:checked").length==1){
			 window.parent.parent.refreshTree();
		}else if (nodes.length>0) {
	
			if(!nodes[0].isParent){
				nodes[0].isParent =true;
				tree.updateNode(nodes[0]);
			}
			tree.reAsyncChildNodes(nodes[0], "refresh");
		}else{
			 window.parent.parent.refreshTree();
		}
		
	}

	
	function batchAdd() {
		var names ='';
		$('#newlist>option').each(function(i,v){
			names += $(this).attr('value')+",";
		});
		if(names == ''){
			return ;
		}
		names = names.substring(0,names.length-1);
		$.ajax({
			url : 'inmp!batchAdd.action', 
			type : 'POST',
			dataType : "json",
			data : {
				parent : $('#parent').attr('value'),
				probeId:$('#probe').attr('value'),
				type:$('#type').attr('value'),
				names:names
			},
			success : function(data) {
				if(data.oper=='success'){
					refreshViewTree();
					getTopWin(window).getMessageBox().tip('保存资源成功', 'success',{timeout: 1000});
					reinit();
					init();
				}else{
					getTopWin(window).getMessageBox().tip('添加资源失败', 'success',{timeout: 1000});
				}
				
				count();
			}
		});
	}
	
	function reinit(){
		$("#validlist>option").remove();
		$("#newlist>option").remove();
	}
	
	
	function init(){
		$.ajax({
			url : 'inmp!itipAvailableRes.action', // 
			type : 'POST',
			dataType : "json",
			data : {
				resType : $('#type').attr('value'),
				probeId:$('#probe').attr('value')
			},
			success : function(data) { //
				$.each(data.valid, function(i, index) {
					$('#validlist').append(
							'<option id='+index.id+' value='+index.name+'>'
							+ index.name + '</option> ');
				});
				
				$.each(data.invalid, function(i, index) {
					$('#validlist').append(
							'<option id='+index.id+' disabled="disabled" value='+index.name+'>'
							+ index.name + '(已集成)</option> ');
				});
				
				count();
			}
		});
	}

     function clickBtn(e) {
         if ("btn_1" == e.target.id) {
             $("#validlist>option[disabled!=disabled]").appendTo("#newlist");
             count();
         } else if ("btn_2" == e.target.id) {
             $("#validlist>option:selected").appendTo("#newlist");
             count();
         } else if ("btn_3" == e.target.id) {
            $("#newlist option:selected").appendTo("#validlist");
            count();
         } else if ("btn_4" == e.target.id) {
             $("#newlist>option").appendTo("#validlist");
             count();
         }else if("back"==e.target.id){
        	 back();
         }else if("save"==e.target.id){
        	 batchAdd();
         }
     }
     
     function count(){
    	 $('#validcnt').text($("#validlist>option[disabled!='disabled']").size());
    	 $('#ingredcnt').text($("#validlist>option[disabled='disabled']").size());
    	 $('#selectcnt').text($("#newlist>option").size());
     }
	
	$(function() {
         $(".btn-4").bind("click", clickBtn);
         init();
	});
</script>

</head>
<body>
	<table class="table_eidt" id="restable" style="margin:10px 50px 10px 50px;width:680px;">
		<tbody>
	    	<tr>
	    		<td class="td_left" style="width:50px;">
					父资源
				</td>
	    		<td id="parent" class="td_right" value="${parent.id }">
	    		 	${parent.name }
				</td>
	    		<td class="td_left" style="width:50px;">
					资源类别
				</td>
	    		<td id="type" class="td_right" value="${resType.id }">
	    		 	${resType.desc }
				</td>
	    		<td class="td_left" style="width:50px;">
					采集器
				</td>
	    		<td id="probe" class="td_right" value="${probe.id }">
	    		 	${probe.name }
				</td>
	    	</tr>
		</tbody>
    </table>
	<div style="margin:10px 50px ;">
		<div style="float:left;width:300px;height: 400px;" >
			<div style="float:left">可集成资源 &nbsp;&nbsp;</div><div style="float:left" id="validcnt"></div>
			<div style="float:left">&nbsp;&nbsp;&nbsp;&nbsp;已集成资源 &nbsp;&nbsp;</div><div style="float:left" id="ingredcnt"></div>
			<select id="validlist" multiple="multiple" style="width:100%;height: 100%;" class="sel" id="sel_left">
			</select>
		</div>
		<div style="float:left;width:60px;margin: 0px 10px;">
			<br/>
			<br />
			<br />
			<br />
			<button class="btn-4" id="btn_1">
				&nbsp;&gt;&gt;&nbsp;
			</button>
			<br />
			<button class="btn-4" id="btn_2">
				&nbsp;&nbsp;&gt;&nbsp;&nbsp;
			</button>
			<br />
			<br />
			<button class="btn-4" id="btn_3">
				&nbsp;&nbsp;&lt;&nbsp;&nbsp;
			</button>
			<br />
			<button class="btn-4" id="btn_4">
				&nbsp;&lt;&lt;&nbsp;
			</button>
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<br />
			<button class="btn-4" id="save">
				保存
			</button>
			<br />
			<br />
			<button class="btn-4" id="back">
				返回
			</button>
		</div>
		<div style="float:left;width:300px;height: 400px;">
			<div style="float:left" >已选择资源列表&nbsp;&nbsp; </div><div style="float:left" id="selectcnt"></div>
			<select id="newlist" multiple="multiple" style="width:100%;height: 100%;" class="sel" id="sel_right">
			</select>
			&nbsp;&nbsp;
		</div>
  	</div>
</body>
</html>
